package views;

import controller.GoodsController;
import controller.UserController;
import entity.Goods;
import entity.User;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GoodsView {
    GoodsController goodsController=new GoodsController();
    UserController userController=new UserController();
    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
    public boolean insertGoodsView(){
        try {
            System.out.println("————————欢迎进入上架商品界面————————");
            System.out.println("请输入您的账号");
            String id=bufferedReader.readLine();
            System.out.println("请输入密保答案：");
            String answer=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (user.getAnswer().equals(answer)){
                System.out.println("请输入想要上架的商品名：");
                String goodsName=bufferedReader.readLine();
                System.out.println("请输入商品编号：");
                String goodsId=bufferedReader.readLine();
                System.out.println("请输入商品定价：");
                String price=bufferedReader.readLine();
                System.out.println("请输入商品库存：");
                String goodsNum=bufferedReader.readLine();
                Goods goods1=new Goods(goodsId,goodsName,Double.parseDouble(price),Integer.parseInt(goodsNum));
                System.out.println(goods1);
                return goodsController.insertGoodsController(goods1);
            }
        }catch (IOException e){
            System.out.println("io异常，上架失败！");
            return false;
        }return false;
    }
    public boolean deleteGoodsView(){
        try {
            System.out.println("—————————欢迎进入下架商品界面————————");
            System.out.println("请输入您的账号");
            String id=bufferedReader.readLine();
            System.out.println("请输入想要下架商品的商品编号：");
            String goodsId=bufferedReader.readLine();
            System.out.println("请输入密保答案：");
            String answer=bufferedReader.readLine();
            User user= userController.selectUserById(Integer.parseInt(id));
            if (user.getAnswer().equals(answer)){
                return goodsController.deleteGoodsByGoodsIdController(goodsId);
            }
        }catch (IOException e){
            System.out.println("io异常，下架失败！");
            return false;
        }
        return false;
    }
    public boolean returnGoodsView(){
        try {
            System.out.println("————————欢迎进入退货界面————————");
            System.out.println("请输入您想要退货的商品名称");
            String goodsName=bufferedReader.readLine();
            Goods goods= goodsController.selectGoodsByNameService(goodsName);
            if (goods.getGoodsName().equals(goodsName)){
                return goodsController.returnGoodsByGoodsNameService(goodsName,goods);
            }
        } catch (Exception e) {
            System.out.println("io异常，退货失败！");
            return false;
        }
        return false;
    }
    public boolean changeGoodsView(){
        try {
            System.out.println("————————欢迎进入换货界面————————");
            System.out.println("请输入您想要换货的商品名称");
            String goodsName=bufferedReader.readLine();
            Goods goods= goodsController.selectGoodsByNameService(goodsName);
            if (goods.getGoodsName().equals(goodsName)){
                return goodsController.changeGoodsByGoodsNameService(goodsName,goods);
            }
        } catch (Exception e) {
            System.out.println("io异常，换货失败！");
            return false;
        }
        return false;
    }
    public boolean buyGoodsView(){
        try {
            System.out.println("————————欢迎进入选购商品界面————————");
            while (true){
                System.out.println("请输入您想要的商品名称");
                String goodsName=bufferedReader.readLine();
                Goods goods=goodsController.selectGoodsByNameService(goodsName);
                if (goods.getGoodsName().equals(goodsName)){
                    System.out.println(goods);
                    System.out.println("如果确认购买请输入是，如果不购买输入其他即可");
                    String buy=bufferedReader.readLine();
                    if (buy.equals("是")){
                        return goodsController.buyGoodsByGoodsNameService(goodsName,goods);
                    }else{
                        continue;
                    }
                }
            }

        } catch (IOException e) {
            System.out.println("io异常，购买失败！");
            return false;
        }
    }

    public boolean updateGoodsView(){
        try {
            System.out.println("—————————欢迎进入更改商品界面————————");
            System.out.println("请输入您的账号");
            String id=bufferedReader.readLine();
            System.out.println("请输入想要更改商品的商品编号");
            String goodsId=bufferedReader.readLine();
            System.out.println("请输入密保答案：");
            String answer=bufferedReader.readLine();
            System.out.println("请输入的商品名称：");
            String goodsName=bufferedReader.readLine();
            System.out.println("请输入商品编号：");
            String goodsId1=bufferedReader.readLine();
            System.out.println("请输入商品价格：");
            String price=bufferedReader.readLine();
            System.out.println("请输入商品库存：");
            String goodsNum=bufferedReader.readLine();
            Goods goods=new Goods(goodsId1,goodsName,Double.parseDouble(price),Integer.parseInt(goodsNum));
            User user= userController.selectUserById(Integer.parseInt(id));
            if (user.getAnswer().equals(answer)){
                return goodsController.updateGoodsByIdService(goodsId,goods);
            }
        }catch (IOException e){
            System.out.println("io异常，更改失败！");
            return false;
        }
        return false;
    }
    public void selectGoodsView(){
        try {
            System.out.println("————————欢迎来到商品查询页面！————————");
            System.out.println("请选择你要选择的查询方式：");
            System.out.println(" 1.查询所有商品 2.根据商品编号查询商品 3.根据商品名查询商品");
            switch (Integer.parseInt(bufferedReader.readLine())){
                case 1:
                    System.out.println("请输入您的账号：");
                    String id1=bufferedReader.readLine();
                    User user=userController.selectUserById(Integer.parseInt(id1));
                    if (user.getLevel()==3||user.getLevel()==1) {
                        System.out.println(goodsController.selectAllGoodsService());
                    }else {
                        System.out.println("抱歉，您没有权限访问！");
                    }
                    break;
                case 2:
                    System.out.println("请输入您的账号：");
                    String id2=bufferedReader.readLine();
                    User user1=userController.selectUserById(Integer.parseInt(id2));
                    if (user1.getLevel()==3||user1.getLevel()==2){
                        System.out.println("请输入您想要查询商品的商品编号：");
                        String goodsid=bufferedReader.readLine();
                        System.out.println(goodsController.selectGoodsByIdController(goodsid));
                    }else {
                        System.out.println("抱歉，您没有权限访问！");
                    }
                    break;
                case 3:
                    System.out.println("请输入您的账号：");
                    String id=bufferedReader.readLine();
                    User user2=userController.selectUserById(Integer.parseInt(id));
                    if (user2.getLevel()==1||user2.getLevel()==3){
                        System.out.println("请输入您想要查询商品的商品名：");
                        String goodsName=bufferedReader.readLine();
                        System.out.println(goodsController.selectGoodsByNameService(goodsName));
                    }else {
                        System.out.println("抱歉，您没有权限访问！");
                    }
                    break;
                default:
                    System.out.println("输入指令错误！");
                    break;
            }
        }catch (IOException e){
            System.out.println("io异常，查询失败！");
        }
    }
}


